<?php

namespace app\api\controller\v2;

use app\api\logic\Money;
use app\api\logic\RateLogic;
use app\common\controller\Api;
use app\common\library\Auth;
use app\common\model\Category;
use app\common\model\User;
use app\common\utils\Utils;
use think\Config;
use think\Db;
use think\Lang;

/**
 * 众包费接口
 */
class Taskmoney extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    public function _initialize()
    {
        parent::_initialize();
    }
    /**
     * 获取众包费信息
     * @param $month 年月份,格式2019-01，默认当月
     * @param $is_sure 状态=0待完成;1=已完成，默认待完成
     * @param $page 默认1
     */
    public function getTaskMoneyList() {
        $month = input('month/s', date('Y-m'));
        $type = input('type/d', 0);

        if( !preg_match('/^[\d]{4}\-[\d]{2}$/', $month) )
        {
            $this->error('时间格式错误', $month);
        }
        $where['user_id'] = UID;
        try{
            $start_timestamp = strtotime($month);
            $end_timestamp = mktime(23, 59, 59, date('m', $start_timestamp)+1, 00, date('Y', $start_timestamp));//指定月份月末时间戳  
            $time_zone = [$start_timestamp, $end_timestamp];
            $where['createtime'] = ['between', $time_zone];
        }catch(\Exception $e)
        {
            $this->error('时间格式不正确');
        }
        $model = Money::instance()->where($where)->setUserId(UID);

        #加载第一页的数据时候返回统计信息
        $page = request()->param('page/d', 1);
        $result['statistics'] = ['yes'=> 0, 'no' => 0];
        if( $page == 1 )
        {
            $result['statistics'] = $model->moneySum();
        }

        $result['list'] = Money::instance()->where($where)->getMoneyList($type);

        $this->success('获取成功', $result);
    }


    /**
     * 获取众包费详细信息
     * @param $id  众包费ID
     */
    public function getTaskMoneyInfo() {
        $id = input('id/d', 0);
        if( !$id ){
            $this->error('参数错误');
        }
        $money = Money::instance()->get($id);
        if( !$money ){
            $this->error('未找到数据');
        }
        $money = Money::instance()->setUserId(UID)->processGet($money);
        $this->success('获取成功', $money);
    }


    /**
     * 获取个税信息
     * @param $month 年月份,格式2019-01，默认当月
     * @param $is_sure 状态=0待完成;1=已完成，默认待完成
     * @param $page 默认1
     */
    public function getTaxList() {
        $month = input('month/s', date('Y-m'));
        if( !preg_match('/^[\d]{4}\-[\d]{2}$/', $month) )
        {
            $this->error('时间格式错误', $month);
        }
        $start_timestamp = strtotime($month);
        $end_timestamp = mktime(23, 59, 59, date('m', $start_timestamp)+1, 00, date('Y', $start_timestamp));//指定月份月末时间戳
        $time_zone = [$start_timestamp, $end_timestamp];

        #加载第一页的数据时候返回统计信息
        $page = request()->param('page/d', 1);
        $rate = RateLogic::instance()->setUserId(UID);
        if( $page == 1 )
        {
            $result['statistics'] = [
                'tax_of_month' => $rate->moneySum(['createtime' => ['between', $time_zone]]),
                'tax_of_year'  => $rate->moneySum(['createtime' => ['between', [ strtotime(date('Y-01-01', $start_timestamp)), strtotime(date('Y-12-31 23:59:59', $start_timestamp)) ] ]]),
            ];
        }

        $result['list'] = $rate->getMoneyList(['createtime' => ['between', $time_zone]]);
        $this->success('获取成功', $result);
    }



}